//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "OptionletVolatilityStructure.h"
using namespace Cephei::QL::Termstructures::Volatility::Optionlet;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
#include <gen/QL/Termstructures/VolatilityTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures::Volatility;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::COptionletVolatilityStructure (boost::shared_ptr<QuantLib::OptionletVolatilityStructure>& childNative, Object^ owner) : CVolatilityTermStructure(COptionletVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phOptionletVolatilityStructure = NULL;
#endif
	_ppOptionletVolatilityStructure = &childNative;
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppOptionletVolatilityStructure));
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::COptionletVolatilityStructure (QuantLib::OptionletVolatilityStructure& childNative, Object^ owner) : CVolatilityTermStructure(COptionletVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phOptionletVolatilityStructure = NULL;
#endif
	_ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (&childNative);
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppOptionletVolatilityStructure));
    _OptionletVolatilityStructureOwner = owner;
    _VolatilityTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::COptionletVolatilityStructure (COptionletVolatilityStructure^ copy) : CVolatilityTermStructure(COptionletVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phOptionletVolatilityStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (copy->GetShared());
        _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppOptionletVolatilityStructure));
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::COptionletVolatilityStructure (System::Type^ t) : CVolatilityTermStructure(COptionletVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phOptionletVolatilityStructure = NULL;
#endif
	if (!t->IsSubclassOf(COptionletVolatilityStructure::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::COptionletVolatilityStructure (QuantLib::Handle<QuantLib::OptionletVolatilityStructure>& childNative, Object^ owner)  : CVolatilityTermStructure(COptionletVolatilityStructure::typeid)
{
	_phOptionletVolatilityStructure = &childNative;
	_ppOptionletVolatilityStructure = &static_cast<boost::shared_ptr<QuantLib::OptionletVolatilityStructure>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppOptionletVolatilityStructure));
    _OptionletVolatilityStructureOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::COptionletVolatilityStructure (QuantLib::Handle<QuantLib::OptionletVolatilityStructure> childNative)  : CVolatilityTermStructure(COptionletVolatilityStructure::typeid)
{
	_phOptionletVolatilityStructure = &childNative;
	_ppOptionletVolatilityStructure = &static_cast<boost::shared_ptr<QuantLib::OptionletVolatilityStructure>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppOptionletVolatilityStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::COptionletVolatilityStructure (QuantLib::OptionletVolatilityStructure childNative)  : CVolatilityTermStructure(COptionletVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phOptionletVolatilityStructure = NULL;
#endif
	_ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (new QuantLib::OptionletVolatilityStructure (childNative));
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppOptionletVolatilityStructure));
}
#endif

Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::~COptionletVolatilityStructure ()
{
    if (_ppOptionletVolatilityStructure != NULL)
    {
	    delete _ppOptionletVolatilityStructure;
        _ppOptionletVolatilityStructure = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::!COptionletVolatilityStructure ()
{
    if (_ppOptionletVolatilityStructure != NULL)
    {
	    delete _ppOptionletVolatilityStructure;
    }
}
QuantLib::OptionletVolatilityStructure& Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::GetReference ()
{
    if (_ppOptionletVolatilityStructure == NULL) throw gcnew NativeNullException ();
	return **_ppOptionletVolatilityStructure;
}
boost::shared_ptr<QuantLib::OptionletVolatilityStructure>& Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::GetShared ()
{
    if (_ppOptionletVolatilityStructure == NULL) throw gcnew NativeNullException ();
	return *_ppOptionletVolatilityStructure;
}
QuantLib::OptionletVolatilityStructure* Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::GetPointer ()
{
    if (_ppOptionletVolatilityStructure == NULL) throw gcnew NativeNullException ();
	return &**_ppOptionletVolatilityStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OptionletVolatilityStructure>& Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::GetHandle ()
{
	if (_phOptionletVolatilityStructure == NULL)
	{
		_phOptionletVolatilityStructure = new Handle<QuantLib::OptionletVolatilityStructure> (*_ppOptionletVolatilityStructure);
	}
	return *_phOptionletVolatilityStructure;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::HasNative () 
{
	return (_ppOptionletVolatilityStructure != NULL);
}

Double Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::BlackVariance (Double optionTime, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _optionTime = (QuantLib::Time)ValueHelper::Convert (optionTime);
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOptionletVolatilityStructure)->blackVariance ( _optionTime,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::BlackVariance (DateTime optionDate, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Date _optionDate = (QuantLib::Date)ValueHelper::Convert (optionDate);
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOptionletVolatilityStructure)->blackVariance ( _optionDate,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::BlackVariance (Cephei::QL::Times::IPeriod^ optionTenor, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _CoptionTenor;
    try
    {
        _CoptionTenor = safe_cast<CPeriod^> (optionTenor);
        _CoptionTenor->Lock();
        QuantLib::Period& _optionTenor = static_cast<QuantLib::Period&> (_CoptionTenor->GetReference ()); 
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOptionletVolatilityStructure)->blackVariance ( _optionTenor,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoptionTenor != nullptr) _CoptionTenor->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::ISmileSection^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::SmileSection (Double optionTime, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extr)
{
    try
    {
        QuantLib::Time _optionTime = (QuantLib::Time)ValueHelper::Convert (optionTime);
        bool _extr = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extr) ? (bool)ValueHelper::Convert (extr->Value) : false); //9a
    	boost::shared_ptr<QuantLib::SmileSection> _rv = (boost::shared_ptr<QuantLib::SmileSection>)(*_ppOptionletVolatilityStructure)->smileSection ( _optionTime,  _extr );   
        Cephei::QL::Termstructures::Volatility::CSmileSection^ _nrv = gcnew Cephei::QL::Termstructures::Volatility::CSmileSection (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::ISmileSection^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::SmileSection (DateTime optionDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extr)
{
    try
    {
        QuantLib::Date _optionDate = (QuantLib::Date)ValueHelper::Convert (optionDate);
        bool _extr = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extr) ? (bool)ValueHelper::Convert (extr->Value) : false); //9a
    	boost::shared_ptr<QuantLib::SmileSection> _rv = (boost::shared_ptr<QuantLib::SmileSection>)(*_ppOptionletVolatilityStructure)->smileSection ( _optionDate,  _extr );   
        Cephei::QL::Termstructures::Volatility::CSmileSection^ _nrv = gcnew Cephei::QL::Termstructures::Volatility::CSmileSection (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::ISmileSection^ Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::SmileSection (Cephei::QL::Times::IPeriod^ optionTenor, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extr)
{
    CPeriod^ _CoptionTenor;
    try
    {
        _CoptionTenor = safe_cast<CPeriod^> (optionTenor);
        _CoptionTenor->Lock();
        QuantLib::Period& _optionTenor = static_cast<QuantLib::Period&> (_CoptionTenor->GetReference ()); 
        bool _extr = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extr) ? (bool)ValueHelper::Convert (extr->Value) : false); //9a
    	boost::shared_ptr<QuantLib::SmileSection> _rv = (boost::shared_ptr<QuantLib::SmileSection>)(*_ppOptionletVolatilityStructure)->smileSection ( _optionTenor,  _extr );   
        Cephei::QL::Termstructures::Volatility::CSmileSection^ _nrv = gcnew Cephei::QL::Termstructures::Volatility::CSmileSection (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoptionTenor != nullptr) _CoptionTenor->Unlock();
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::Volatility (Double optionTime, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _optionTime = (QuantLib::Time)ValueHelper::Convert (optionTime);
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppOptionletVolatilityStructure)->volatility ( _optionTime,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::Volatility (DateTime optionDate, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Date _optionDate = (QuantLib::Date)ValueHelper::Convert (optionDate);
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppOptionletVolatilityStructure)->volatility ( _optionDate,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::COptionletVolatilityStructure::Volatility (Cephei::QL::Times::IPeriod^ optionTenor, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _CoptionTenor;
    try
    {
        _CoptionTenor = safe_cast<CPeriod^> (optionTenor);
        _CoptionTenor->Lock();
        QuantLib::Period& _optionTenor = static_cast<QuantLib::Period&> (_CoptionTenor->GetReference ()); 
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppOptionletVolatilityStructure)->volatility ( _optionTenor,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoptionTenor != nullptr) _CoptionTenor->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

